package interfaces.spawnMenu;

import logic.common.player.Player;
import logic.common.player.playerListeners.PlayerTeamListener;
import logic.common.player.playerListeners.PlayerValueListener;
import logic.common.team.Team;
import logic.status.StatusChangedEvent;
import main.AbsInitGame;
import main.InitGame;
import networking.messages.syncMessages.ValueType;
import interfaces.GUISource;
import interfaces.language.LanguageButton;
import interfaces.language.LanguageWindow;
import interfaces.superWidgets.InterfaceLabel;
import fileHandling.language.interfaces.SpawnMenuText;
import gameStates.GameTimeListener;

import org.fenggui.binding.render.ImageFont;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.layout.StaticLayout;

public class SelectWindow extends LanguageWindow {
	
	private AbsSpawnMenu spawnMenu;
	
	private LanguageButton switchTeamButton, tacticButton, customButton, spawnMapButton;
	private int smallHeight, bigHeight, buttonWidth, buttonX;
	private InterfaceLabel spawnTimeLabel;
	private ReadyButton readyButton;
	
	public SelectWindow(final AbsSpawnMenu spawnMenu) {
		super(false, SpawnMenuText.Configure);
		this.spawnMenu = spawnMenu;
		
		content.setLayoutManager(new StaticLayout());
		
		int height = spawnMenu.getHeight() - spawnMenu.getHeight() / 6;
		int width = spawnMenu.getWidth() / 5;
		int border = spawnMenu.getHeight() / 12;
		
		setSize(width, height);
		setXY(spawnMenu.getWidth() - width - border, border);
		layout();
		
		smallHeight = content.getHeight() / 18;
		bigHeight = (content.getHeight() - (2 * smallHeight)) / 3;
		buttonWidth = content.getWidth();
		buttonX = 0;
		
		makeSwitchTeamButton();
		makeCenterButtons();
		makeSpawnButton();
		
		final Player player = spawnMenu.getGame().getCurrentPlayer();
		player.getTeam().getIngameState().addGameTimeListener(new GameTimeListener() {
			@Override
			public void respawnTimeUpdate() {
				if(player.getSpawnTime() == 0) spawnTimeLabel.setText(" ");
				else spawnTimeLabel.setText(player.getSpawnTime() + "");
			}
			
			@Override
			public void gameTimeUpdate(Integer newGameTime) {}
			@Override
			public void endTimeUpdate(Integer newEndTime) {}
		});
		
		spawnMenu.addListener(player, new PlayerValueListener() {
			@Override
			public void valueChanged(StatusChangedEvent event) {
				if(event.getType() == ValueType.Deaths) {
					SelectWindow.this.spawnMenu.setReady(false);
					readyButton.setReady(false);
				}
			}
		});
		
		spawnMenu.addListener(player, new PlayerTeamListener() {
			@Override
			public void teamChanged(Team newTeam) {
				spawnMenu.changeSelectedTeam(newTeam);
			}
			
			@Override
			public void teamChangeFailed() {
				TeamChangeFailedWindow window = new TeamChangeFailedWindow();
				spawnMenu.addWidget(window);
			}
		});
		
		layout();
	}
	
	private void makeSwitchTeamButton() {
		ImageFont buttonFont = GUISource.bigFont;
		
		switchTeamButton = new LanguageButton(SpawnMenuText.Switch_Team, buttonFont);
		switchTeamButton.setSize(buttonWidth, smallHeight);
		switchTeamButton.setXY(buttonX, content.getHeight() - switchTeamButton.getHeight());
		switchTeamButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				Player player = spawnMenu.getGame().getCurrentPlayer();
				AbsInitGame init = InitGame.get();
				
				if(init.isServer()) {
					if(spawnMenu.canSwitchTeam()) player.switchTeam(player.getEnemyTeam());
					else player.teamChangeFailed();
				} else if(!init.isServer()) 
					init.getClientState().playerWantsTeamChange(player);
			}
		});
		
		content.addWidget(switchTeamButton);
	}
	
	private void makeCenterButtons() {
		tacticButton = new LanguageButton(SpawnMenuText.Squads, GUISource.bigFont);
		tacticButton.setSize(buttonWidth, bigHeight);
		tacticButton.setXY(buttonX, switchTeamButton.getY() - tacticButton.getHeight());
		tacticButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				spawnMenu.setCurrentWindow(spawnMenu.getTacticWindow());
			}
		});
		content.addWidget(tacticButton);
		
		customButton = new LanguageButton(SpawnMenuText.Customize, GUISource.bigFont);
		customButton.setSize(buttonWidth, bigHeight);
		customButton.setXY(buttonX, tacticButton.getY() - customButton.getHeight());
		customButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				spawnMenu.setCurrentWindow(spawnMenu.getCustomWindow());
			}
		});
		content.addWidget(customButton);
		
		spawnMapButton = new LanguageButton(SpawnMenuText.Spawn_Map, GUISource.bigFont);
		spawnMapButton.setSize(buttonWidth, bigHeight);
		spawnMapButton.setXY(buttonX, customButton.getY() - spawnMapButton.getHeight());
		spawnMapButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				spawnMenu.setCurrentWindow(spawnMenu.getSpawnMap());
			}
		});
		content.addWidget(spawnMapButton);
	}
	
	private void makeSpawnButton() {
		readyButton = new ReadyButton(SpawnMenuText.Ready, GUISource.bigFont);
		int height = spawnMapButton.getY();
		readyButton.setSize(buttonWidth / 6 * 5, height);
		readyButton.setXY(buttonX, 0);
		readyButton.addButtonPressedListener(new IButtonPressedListener() {
			@Override
			public void buttonPressed(ButtonPressedEvent e) {
				if(spawnMenu.isReady()) {
					spawnMenu.setReady(false);
					readyButton.setReady(false);
				} else if(spawnMenu.getSelectedHunter() != null && spawnMenu.getSelectedSpawnLocation() != null) {
					spawnMenu.setReady(true);
					readyButton.setReady(true);
					Player player = spawnMenu.getGame().getCurrentPlayer();
					if(!player.isAlive() && player.getSpawnTime() == 0) 
						player.getTeam().getIngameState().spawnCurrentPlayer();
				}
			}
		});
		content.addWidget(readyButton);
		
		spawnTimeLabel = new InterfaceLabel(" ", GUISource.bigFont);
		spawnTimeLabel.setSize(buttonWidth - readyButton.getWidth(), height);
		spawnTimeLabel.setXY(buttonX + readyButton.getWidth(), 0);
		content.addWidget(spawnTimeLabel);
	}
	
	public void teamChanged() {
		readyButton.setReady(false);
	}
	
	public ReadyButton getReadyButton() { return readyButton; }
}